package me.xuym.crawler.simpleextension.filter;

import com.cbs.java.component.application.Application;
import me.xuym.crawler.Processor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by broche on 05/08/2017.
 */
public abstract class SimpleCachedListFilter extends Processor<List<SimpleCachedFilterInput>, List<SimpleCachedFilterInput>> {
    protected abstract boolean isCacheExisted(Map<String, String> fields);

    protected abstract void saveCache(Map<String, String> fields);

    public SimpleCachedListFilter(Application application) throws IOException {
        super(application);
    }

    @Override
    public List<SimpleCachedFilterInput> onProcess(List<SimpleCachedFilterInput> simpleCachedFilterInputs) throws Exception {
        List<SimpleCachedFilterInput> result = new ArrayList<>();
        for (SimpleCachedFilterInput input : simpleCachedFilterInputs) {
            if (isCacheExisted(input.simpleCachedFilterCheckFields())) {
                continue;
            } else {
                result.add(input);
            }
        }
        for (SimpleCachedFilterInput input : result) {
            saveCache(input.simpleCachedFilterSaveFields());
        }
        return result;
    }
}
